<template>
  <div class="drag-test">
    <h2>拖拽测试页面</h2>
    <p>测试跨层级拖拽功能</p>

    <div class="test-container">
      <div class="source-area">
        <h3>源区域</h3>
        <div
          v-for="item in sourceItems"
          :key="item.id"
          class="draggable-item"
          draggable="true"
          @dragstart="handleDragStart($event, item)"
          @dragend="handleDragEnd"
        >
          <span class="drag-icon">⋮⋮</span>
          <span class="item-name">{{ item.name }}</span>
          <span class="item-level">L{{ item.level }}</span>
        </div>
      </div>

      <div class="target-area">
        <h3>目标区域</h3>
        <div
          v-for="item in targetItems"
          :key="item.id"
          class="droppable-item"
          @dragover="handleDragOver"
          @dragenter="handleDragEnter($event, item)"
          @dragleave="handleDragLeave"
          @drop="handleDrop($event, item)"
          :class="{ 'drop-target': dropTarget?.id === item.id }"
        >
          <span class="item-name">{{ item.name }}</span>
          <span class="item-level">L{{ item.level }}</span>
          <div v-if="item.children" class="children">
            <div
              v-for="child in item.children"
              :key="child.id"
              class="child-item"
              @dragover="handleDragOver"
              @dragenter="handleDragEnter($event, child)"
              @dragleave="handleDragLeave"
              @drop="handleDrop($event, child)"
              :class="{ 'drop-target': dropTarget?.id === child.id }"
            >
              <span class="item-name">{{ child.name }}</span>
              <span class="item-level">L{{ child.level }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="log-area">
      <h3>操作日志</h3>
      <div class="log-content">
        <div v-for="(log, index) in logs" :key="index" class="log-item">
          {{ log }}
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { message } from 'ant-design-vue';

const sourceItems = ref([
  { id: 's1', name: '源项目1', level: 1 },
  { id: 's2', name: '源项目2', level: 2 },
  { id: 's3', name: '源项目3', level: 3 },
]);

const targetItems = ref([
  {
    id: 't1',
    name: '目标项目1',
    level: 1,
    children: [
      { id: 't1.1', name: '子项目1.1', level: 2 },
      { id: 't1.2', name: '子项目1.2', level: 2 },
    ],
  },
  {
    id: 't2',
    name: '目标项目2',
    level: 1,
    children: [{ id: 't2.1', name: '子项目2.1', level: 2 }],
  },
]);

const draggedItem = ref(null);
const dropTarget = ref(null);
const logs = ref<string[]>([]);

const addLog = (message: string) => {
  logs.value.unshift(`${new Date().toLocaleTimeString()}: ${message}`);
  if (logs.value.length > 10) {
    logs.value.pop();
  }
};

const handleDragStart = (e: DragEvent, item: any) => {
  draggedItem.value = item;
  e.dataTransfer!.effectAllowed = 'move';
  e.dataTransfer!.setData('text/plain', item.id);
  addLog(`开始拖拽: ${item.name} (L${item.level})`);
};

const handleDragOver = (e: DragEvent) => {
  e.preventDefault();
  e.dataTransfer!.dropEffect = 'move';
};

const handleDragEnter = (e: DragEvent, item: any) => {
  e.preventDefault();
  dropTarget.value = item;
  addLog(`悬停在: ${item.name} (L${item.level})`);
};

const handleDragLeave = (e: DragEvent) => {
  e.preventDefault();
  dropTarget.value = null;
};

const handleDrop = (e: DragEvent, targetItem: any) => {
  e.preventDefault();

  if (draggedItem.value && targetItem) {
    addLog(`成功拖拽: ${draggedItem.value.name} -> ${targetItem.name}`);
    message.success(
      `成功将 ${draggedItem.value.name} 移动到 ${targetItem.name} 下`,
    );
  }

  draggedItem.value = null;
  dropTarget.value = null;
};

const handleDragEnd = (e: DragEvent) => {
  e.preventDefault();
  draggedItem.value = null;
  dropTarget.value = null;
  addLog('拖拽结束');
};
</script>

<style scoped>
.drag-test {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.test-container {
  display: flex;
  gap: 40px;
  margin: 20px 0;
}

.source-area,
.target-area {
  flex: 1;
  border: 2px solid #f0f0f0;
  border-radius: 8px;
  padding: 20px;
  min-height: 400px;
}

.draggable-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  margin: 8px 0;
  background: white;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: grab;
  transition: all 0.2s;
}

.draggable-item:hover {
  background: #f5f5f5;
  border-color: #1890ff;
}

.draggable-item:active {
  cursor: grabbing;
}

.droppable-item {
  padding: 12px;
  margin: 8px 0;
  background: white;
  border: 2px solid #f0f0f0;
  border-radius: 6px;
  transition: all 0.2s;
}

.droppable-item.drop-target {
  background: #f6ffed;
  border-color: #52c41a;
  box-shadow: 0 0 8px rgba(82, 196, 26, 0.3);
}

.child-item {
  margin: 8px 0 8px 20px;
  padding: 8px;
  background: #fafafa;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  transition: all 0.2s;
}

.child-item.drop-target {
  background: #f6ffed;
  border-color: #52c41a;
}

.drag-icon {
  color: #999;
  font-size: 16px;
  cursor: grab;
}

.item-name {
  flex: 1;
  font-weight: 500;
}

.item-level {
  color: #666;
  font-size: 12px;
  background: #f0f0f0;
  padding: 2px 6px;
  border-radius: 3px;
}

.log-area {
  margin-top: 20px;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  padding: 16px;
}

.log-content {
  max-height: 200px;
  overflow-y: auto;
  background: #f5f5f5;
  padding: 12px;
  border-radius: 4px;
}

.log-item {
  padding: 4px 0;
  font-family: monospace;
  font-size: 12px;
  color: #333;
}
</style>
